package com.ypinkj.app.service.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author LF
 * @date 2020/10/9 - 17:18
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;	//Spring自动注入RedisTemplate

    @Override
    public void addKey(String key, String value) {
        // 600: 表示该数据在缓存中存在的时间，TimeUnit.SECONDS为单位秒，
        // 600  秒后缓存中的数据会自动消失
        redisTemplate.opsForValue().set(key,value,600, TimeUnit.SECONDS);
    }

    @Override
    public String getValueByKey(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }
}
